package com.qyzmt.controller;

import com.qyzmt.pojo.IndexVideo;
import com.qyzmt.pojo.LoginToken;
import com.qyzmt.pojo.Result;
import com.qyzmt.pojo.Volunteer;
import com.qyzmt.service.ManageSystemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/api")
@RestController
public class ManageSystemController {

    @Autowired
    private ManageSystemService manageSystemService;

    // 认证
    @PostMapping("/admin/verify")
    public Result verify(@RequestBody LoginToken loginToken){
        // 前台验证 authid 与 authtoken
        int authid = Integer.parseInt(loginToken.getAuthid());
        int authtoken = Integer.parseInt(loginToken.getAuthtoken());
        log.info("用户访问后台管理系统:authid{},authtoken{}",authid,authtoken);
        if((authid - 7) * 2  == authtoken){
            log.info("AuthId与AuthToken验证成功!");
            return Result.success();
        }
        else{
            log.info("AuthId与AuthToken验证失败!");
            return Result.error("登录失败！AuthID认证失败！");
        }
    }

    // 添加视频
    @PostMapping("/videos")
    public Result addVideo(@RequestBody IndexVideo indexVideo){
        int count = manageSystemService.addVideo(indexVideo);
        if(count != 0){
            log.info("管理添加视频成功:{},返回{}",indexVideo,count);
            return Result.success();
        }
        else{
            log.info("管理添加视频失败！{},返回{}",indexVideo,count);
            return Result.error("插入失败！");
        }
    }

    // 修改视频
    @PutMapping("/videos/{id}")
    public Result updateVideo(@PathVariable int id, @RequestBody IndexVideo indexVideo){
        int count = manageSystemService.updateVideo(id, indexVideo);
        if(count != 0){
            log.info("管理修改视频成功:{},{},返回{}",id,indexVideo,count);
            return Result.success();
        }
        else{
            log.info("管理修改视频失败！{},{},返回{}",id,indexVideo,count);
            return Result.error("修改失败！");
        }
    }

    // 删除视频
    @DeleteMapping("/videos/{id}")
    public Result deleteVideo(@PathVariable int id){
        int count = manageSystemService.deleteVideo(id);
        if(count != 0){
            log.info("管理删除视频成功:{},返回{}",id,count);
            return Result.success();
        }
        else{
            log.info("管理删除视频失败！{},返回{}",id,count);
            return Result.error("删除失败！");
        }

    }

    // 删除志愿者
    @PostMapping("/volunteers/delete")
    public Result deleteVolunteer(@RequestBody Volunteer volunteer){
        int count = manageSystemService.deleteVolunteer(volunteer);
        if(count != 0){
            log.info("管理删除志愿者成功:{},返回{}",volunteer,count);
            return Result.success();
        }
        else{
            log.info("管理删除志愿者失败！{},返回{}",volunteer,count);
            return Result.error("删除失败！");
        }
    }

}
